package org.jeecg.iot.nettyServer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class DeviceProtocol {
    String header;
    byte length;
    byte protocolType;
    byte deviceSn;
    byte deviceType;
    byte version;
    private byte[] payload;
    private byte checksum;
    public DeviceProtocol() {
    }
    public DeviceProtocol(String header,byte version,
                          byte protocolType,byte deviceSn,byte length,
                          byte deviceType,
                          byte[] payload,byte checksum) {
        this.header = header;
        this.version = version;
        this.protocolType = protocolType;
        this.deviceSn = deviceSn;
        this.deviceType = deviceType;
        this.length = length;
        this.payload = payload;
        this.checksum = checksum;
    }

    public static void writeMessage(ChannelHandlerContext ctx, byte[] obj) {

        ByteBuf bufff = Unpooled.buffer();//netty需要用ByteBuf传输
        bufff.writeBytes(obj);//对接需要16进制
        //获取当前的handle
        ctx.writeAndFlush(bufff).addListener((ChannelFutureListener) future -> {
            StringBuilder sb = new StringBuilder("");
            if (future.isSuccess()) {
                log.info(sb.toString() + "回写成功.");
            } else {
                log.error(sb.toString() + "回写失败.");
            }
        });
    }
}
